<?php
namespace Managers\Utils;
require_once "sphinxapi.php";

use Zend_Registry;

class SphinxManager
{
    /**
     * @var \SphinxClient
     */
    protected $_sphinx;

    static private $_max = 9999999;
    static private $_min = 0;

    public function __construct()
    {

        $sphinxHost = Zend_Registry::get('config')->sphinx->host;
        $sphinxPort = (int)Zend_Registry::get('config')->sphinx->port;

        $this->_sphinx = new \SphinxClient();
        $this->_sphinx->SetServer($sphinxHost, $sphinxPort);
        $this->_sphinx->SetArrayResult(true);

        return $this;
    }

    public function whereFrom($field, $value, $exclude = false)
    {
        $this->_sphinx->SetFilterRange($field, $value, self::$_max, $exclude);
        return $this;
    }

    public function setRange($field, $start, $end)
    {
        $this->_sphinx->SetFilterRange($field, $start, $end);
        return $this;
    }

    public function whereFromToFloat($field, $from, $to)
    {
        $this->_sphinx->SetFilterFloatRange($field, (float)$from, (float)$to);
        return $this;
    }

    public function whereTo($field, $value, $exclude = false)
    {
        $this->_sphinx->SetFilterRange($field, self::$_min, $value, $exclude);
        return $this;
    }

    public function addEq($field, $value)
    {
        $this->_sphinx->SetFilter($field, $value);
        return $this;
    }

    public function addNotEq($field, $value)
    {
        $this->_sphinx->SetFilter($field, $value, true);
        return $this;
    }

    public function orderBy($field, $desc = false)
    {
        $this->_sphinx->SetSortMode($desc ? SPH_SORT_ATTR_ASC : SPH_SORT_ATTR_DESC, $field);
        //        $cl->SetSortMode(SPH_SORT_EXTENDED, 'visits DESC, relev DESC');
        return $this;
    }

    public function orderExtend($string)
    {
        $this->_sphinx->SetSortMode(SPH_SORT_EXTENDED, $string);
        return $this;
    }

    public function limit($offset, $limit, $max = 1000)
    {
        //якщо не вистачае дефаулт лимита - збільшуєм ліміт на +1 сторінку
        if (($offset + $limit) > $max) $max = $offset + $limit + $limit;
        $this->_sphinx->SetLimits($offset, $limit, $max);
        return $this;
    }

    public function addFilter($field = '')
    {
        if (!$field) return false;
        $this->_sphinx->SetGroupBy($field, SPH_GROUPBY_ATTR, '@count desc');
        //        $this->_sphinx->AddQuery('');
    }

    public function addQuery($term, $where = '*')
    {
        $this->_sphinx->AddQuery($term, $where);
        return $this;
    }

    public function getResult()
    {
        $res = $this->_sphinx->RunQueries();
        return $res;
    }

    public function setSelect($query)
    {
        $this->_sphinx->SetSelect($query);
        return $this;
    }

    public function getError()
    {
        return $this->_sphinx->GetLastError();
    }

    public function setAnyMatchMode()
    {
        $this->_sphinx->SetMatchMode(SPH_MATCH_ANY);
        return $this;
    }

    public function setGeoAnchor($user_lat, $user_lon, $item_lat = 'lat', $item_lon = 'lon')
    {
        $this->_sphinx->SetGeoAnchor($item_lat, $item_lon, (float)$user_lat, (float)$user_lon);
        return $this;

    }

    public function setFieldWeights($weights)
    {
        $this->_sphinx->SetFieldWeights($weights);
        return $this;
    }
}
